<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Hello WebSocket</title>
    <link href="webjars/bootstrap/css/bootstrap.min.css" rel="stylesheet">
    <link href="webjars/app/css/main.css" rel="stylesheet">
    <script src="webjars/jquery/jquery.min.js"></script>
    <script src="webjars/sockjs-client/sockjs.min.js"></script>
    <script src="webjars/stomp-websocket/stomp.js"></script>

    <script type="text/javascript">
        var stompClient = null;

        function setConnected(connected) {
            $("#connect").prop("disabled", connected);
            $("#disconnect").prop("disabled", !connected);
            if (connected) {
                $("#conversation").show();
            }
            else {
                $("#conversation").hide();
            }
            $("#greetings").html("");
        }

        function connect() {
            var socket = new SockJS('/webSocket/webSocketEndPoint');
            // var socket = new WebSocket('/webSocket/webSocketEndPoint');
            stompClient = Stomp.over(socket);
            var headers={
                username:$("#username").val(),
                password:$("#password").val()
            };
            stompClient.connect(headers, function (frame) {
                setConnected(true);
                console.log('Connected: ' + frame);
                stompClient.subscribe('/topic/demo2/greetings', function (greeting) {
                    showGreeting(JSON.parse(greeting.body).userId, JSON.parse(greeting.body).content);
                });
            });
        }

        function disconnect() {
            if (stompClient !== null) {
                stompClient.disconnect();
            }
            setConnected(false);
            console.log("Disconnected");
        }

        function sendName() {
            stompClient.send("/app/demo2/hello/10086", {}, JSON.stringify({'message': $("#message").val()}));
        }

        function showGreeting(userId, message) {
            $("#greetings").append("<tr><td> Hi, 这是来自 " + userId + " 的消息: " + message + "</td></tr>");
        }

        $(function () {
            $("form").on('submit', function (e) {
                e.preventDefault();
            });
            $("#connect").click(function () {
                connect();
            });
            $("#disconnect").click(function () {
                disconnect();
            });
            $("#send").click(function () {
                sendName();
            });
        });
    </script>
    <!--
    <script src="http://cdn.bootcss.com/sockjs-client/1.1.1/sockjs.min.js"></script>
    <script src="http://cdn.bootcss.com/stomp.js/2.3.3/stomp.js"></script>
    <script src="http://cdn.bootcss.com/jquery/3.1.1/jquery.min.js"></script>
    -->
</head>
<body>
<noscript><h2 style="color: #ff0000">Seems your browser doesn't support Javascript! Websocket relies on Javascript being
    enabled. Please enable
    Javascript and reload this page!</h2></noscript>
<div id="main-content" class="container">

    <div class="row">
        <div class="col-md-12">
            <form class="form-inline">
                <div class="form-group">
                    <label for="username">用户名：</label>
                    <input type="text" id="username" class="form-control" placeholder="username">
                </div>
                <div class="form-group">
                    <label for="password">密码：</label>
                    <input type="password" id="password" class="form-control" placeholder="password">
                </div>
                <button id="connect" class="btn btn-default" type="submit">登录</button>
                <button id="disconnect" class="btn btn-default" type="submit" disabled="disabled">退出</button>
            </form>
        </div>
    </div>

    <div class="row">
        <div class="col-md-12">
            <form class="form-inline">
                <div class="form-group">
                    <label for="message">消息：</label>
                    <input type="text" id="message" class="form-control" placeholder="请输入...">
                </div>
                <button id="send" class="btn btn-default" type="submit">发送</button>
            </form>
        </div>
    </div>
    <div class="row">
        <div class="col-md-12">
            <table id="conversation" class="table table-striped">
                <thead>
                <tr>
                    <th>来自服务器的群发信息：</th>
                </tr>
                </thead>
                <tbody id="greetings">
                </tbody>
            </table>
        </div>
    </div>
</div>
</body>
</html>
